package com.itrident.filter;

import com.intellij.codeInsight.completion.CompletionParameters;
import com.intellij.codeInsight.completion.CompletionResult;
import com.intellij.codeInsight.completion.CompletionResultSet;
import com.intellij.codeInsight.completion.CompletionSorter;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.template.*;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.PsiClassImpl;
import com.itrident.loader.Loader;
import com.itrident.loader.imlp.MultipleLoader;
import com.itrident.matcher.MultipleMatcher;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * @author cxx
 * @create 2020-12-28 20:02
 **/
public class PatternLiveTemplate extends CustomLiveTemplateBase {
    @Nullable
    @Override
    public String computeTemplateKey(@NotNull CustomTemplateCallback callback) {
        String text = callback.getContext ().getText ();
        return text;
    }

    @Override
    public boolean isApplicable(@NotNull CustomTemplateCallback customTemplateCallback, int i, boolean b) {

        if (!"JAVA".equals (customTemplateCallback.getFileType ().getName ())) {
            return false;
        }

        PsiElement[] children = customTemplateCallback.getFile ().getChildren ();
        if(children.length > 0){
            for (PsiElement psiElement : children) {
                if (psiElement instanceof PsiClassImpl) {
                    PsiElement elementAt = psiElement.getContext ().findElementAt (i);
                    if(elementAt != null){
                        PsiElement context = elementAt.getContext ();
                        if(context != null){
                            PsiElement parent = context.getParent ();
                            return parent instanceof PsiTypeElement;
                        }
                    }
                }
            }
        }

        return true;
    }

    @Override
    public boolean supportsWrapping() {
        return false;
    }

    @Override
    public void expand(@NotNull String s, @NotNull CustomTemplateCallback customTemplateCallback) {
    }

    @Override
    public void wrap(@NotNull String s, @NotNull CustomTemplateCallback customTemplateCallback) {
    }

    @NotNull
    @Override
    public String getTitle() {
        return null;
    }

    @Override
    public char getShortcut() {
        return 0;
    }

    @Override
    public void addCompletions(CompletionParameters parameters, CompletionResultSet result) {
        String prefix = result.getPrefixMatcher ().getPrefix ();
        List<LookupElement> elements = loadElements(prefix);
        for (LookupElement element : elements) {
            CompletionResult completionResult = CompletionResult.wrap (element, MultipleMatcher.create (prefix),CompletionSorter.emptySorter ());
            if (completionResult != null) {
                result.passResult (completionResult);
            }
        }
        super.addCompletions (parameters, result);
    }

    private List<LookupElement> loadElements(String prefix) {
        Loader loader = new MultipleLoader (this);
        return loader.load (prefix);
    }
}
